﻿using System;
using FrameCore.LogSystem;
using MongoDB.Driver;
using MongoDB.Bson;

namespace Server.Module.MonggoDB
{
    public class DataBaseMoudle : ModuleBase
    {
        public DataBaseMoudle(EModuleType moduleType) : base(moduleType)
        {
        }

        private const string Host = "172.16.177.212"; // 替换成内网IP地址
        private const int Port = 27017; // 替换成内网端口
        private const string Username = "root";
        private const string Password = "ne6Dmy@hPjNeFJ^God48GFXhm";
        private const string AuthSource = "admin";
        private const string Database = "local";
        private const string Collection = "user";

        private MongoClient mongoClient;
        
        
        #region 生命周期
        public override void Init()
        {
            base.Init();
            //因为这个密码有特殊符号@所以使用Uri.EscapeDataString 方法对用户名和密码进行 URL 编码
            string encodedUsername = Uri.EscapeDataString(Username);
            string encodedPassword = Uri.EscapeDataString(Password);
            // MongoDB 连接字符串
            string connectionString = $"mongodb://{encodedUsername}:{encodedPassword}@{Host}:{Port}/?authSource={AuthSource}";
            // 创建 MongoDB 客户端
            mongoClient = new MongoClient(connectionString);
            Log.Info($"数据库实例化成功==={mongoClient}");
        }

        public override void Start()
        {
            base.Start();
        }

        public override void EarlyUpdate()
        {
            base.EarlyUpdate();
        }

        public override void Update()
        {
            base.Update();
        }

        public override void LateUpdate()
        {
            base.LateUpdate();
        }

        public override void End()
        {
            base.End();
        }

        public override void Release()
        {
            base.Release();
        }
        #endregion
        
    }   
}